## Probability of code error (an upper bound) 
## and error-reduction factor

'''
n is the lenght of the code
t is the error-correcting capacity
p is the probability of a bit error

P_e(n,t,p) gives the probability that t+1 or more
errors occur in the transmission of a code vector.

erf(n,t,p) gives an upper bound for the average number
of errors produced using coding per error produced
without coding
'''

from PyM import *

def P_e(n,t,p):
    err = 0
    for j in range(t+1,n+1):
        err += binom(n,j) * p**j * (1-p)**(n-j)
    return round(err,10)

def erf(n,t,p):
    if p==0: return 1
    return round(P_e(n,t,p)/p,10)
    

p = 0.01
n = 3; t = 1
show(P_e(n,t,p), erf(n,t,p))

n = 7; t = 1
show(P_e(n,t,p), erf(n,t,p))

n = 23; t = 3
show(P_e(n,t,p), erf(n,t,p))